<!DOCTYPE html>
<html lang="en-us" id="extr-page">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" media="screen" href="../../global/css/bootstrap.min.css">
    <link rel="stylesheet" href="../../global/css/ztree/zTreeStyle/zTreeStyle.css" type="text/css">
    <link rel="stylesheet" href="../../global/css/ztree/demo.css" type="text/css">
</head>
<body>
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
    <form class="form-horizontal" onsubmit="return false" id="form"  enctype="multipart/form-data">
        <fieldset>
            <input type="hidden" id="goodsId" name="goodsId">
            <div class="form-group">
                <label class="col-md-2 control-label">商品名称</label>
                <div class="col-md-10">
                    <input class="form-control" placeholder="名称" type="text" name="goodsName" id="goodsName"
                           data-bv-notempty="true"
                           data-bv-notempty-message="商品名称不能为空">
                </div>
            </div>

            <div class="form-group">
                <label class="col-md-2 control-label">商品描述</label>
                <div class="col-md-10">
                    <textarea class="form-control" placeholder="描述" name="goodsDepict" id="goodsDepict"></textarea>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">商品原价</label>
                <div class="col-md-10">
                    <input type="text" class="form-control" placeholder="原价" name="originalPrice" id="originalPrice" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">商品现价</label>
                <div class="col-md-10">
                    <input type="text" class="form-control" placeholder="现价" name="presentPrice" id="presentPrice" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">零钱</label>
                <div class="col-md-10">
                    <input type="text" class="form-control" placeholder="零钱" name="pocketMoney" id="pocketMoney" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">配送费</label>
                <div class="col-md-10">
                    <input type="text" class="form-control" placeholder="配送费" name="postFee" id="postFee" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">品牌</label>
                <div class="col-md-10">
                    <select id="brandId" class="form-control" name="goodsBrandId">
                    	<option value="">请选择</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">供货商</label>
                <div class="col-md-10">
                    <select id="supplierId" class="form-control" name="supplierId">
                    	<option value="">请选择</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">商品状态</label>
                <div class="col-md-10">
                    <select id="goodsStatus" class="form-control" name="goodsStatus">
                    	<option value="">请选择</option>
                    	<option value="0">正常</option>
                    	<option value="1">推荐到首页</option>
                    	<option value="2">禁用/下架</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">规格参数</label>
                <div class="col-md-10">
                    <input type="text" class="form-control" placeholder="规格参数" name="goodsSpec" id="goodsSpec" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">商品规格</label>
                <div class="col-md-10" id="col-md-10">
                    <input type="text"  placeholder="规格明细" name="specDetail" /><input type="text"  placeholder="库存数量" name="specNum" /><input type="text"  placeholder="排序" name="specSort" /><input type="button" class="addSpec" value="新增规格" />
                	
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">商品图片</label>
                <div class="col-md-10">
                <img id="img" />
                    <input type="file" name="goodsBanner2" id="goodsBanner2" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">商品缩略图</label>
                <div class="col-md-10">
                <img id="img2" />
                    <input type="file" name="goodsPic2" id="goodsPic2" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-md-2 control-label">分类</label>
                <div class="col-md-10">
                    <ul id="treeDemo2" class="ztree"></ul>
                </div>
            </div>

            <div class="form-actions">
                <div class="row" align="center">
                    <div class="col-md-12">
                        <button class="btn btn-primary" onclick="location.href='scmList.html'">返回</button>
                        <button id="submitbtn" class="btn btn-primary" type="submit" onclick="add()">
                            <i class="fa fa-save"></i> 保存
                        </button>
                    </div>
                </div>
            </div>

        </fieldset>
    </form>
</div>

<script type="text/javascript" src="../../global/js/libs/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="../../global/js/jq.js"></script>
<script type="text/javascript" src="../../global/js/plugin/bootstrapvalidator/bootstrapValidator.min.js"></script>
<script type="text/javascript" src="../../global/js/common.js"></script>
<script type="text/javascript" src="../../global/layui/layui.js"></script>
<script type="text/javascript" src="../../global/js/libs/jquery.ztree.all-3.5.min.js"></script>
<script type="text/javascript" src="../../global/js/ajaxfileupload.js"></script>
<script type="text/javascript" src="../../global/js/my/ztree-menu.js"></script>
<script type="text/javascript">
    layui.use('layer', function () {
        var layer = layui.layer;
    });

    $.fn.zTree.init($("#treeDemo2"), getSettting2(), getMenuTree3());
    $.ajax({
        type: 'get',
        url: '/clyjGoodsBrand/getBrand2',
        contentType: "application/json; charset=utf-8",
        async: false,
        success: function (data) {
        	console.info(data);
        	for(var i=0;i<data.length;i++){
        		$("#brandId").append("<option value='"+data[i].brandId+"'>"+data[i].brandName+"</option>");
        	}
        }
    });
    $.ajax({
        type: 'get',
        url: '/clyjGoods/getSupplier',
        contentType: "application/json; charset=utf-8",
        async: false,
        success: function (data) {
        	console.info(data);
        	for(var i=0;i<data.length;i++){
        		$("#supplierId").append("<option value='"+data[i].id+"'>"+data[i].username+"</option>");
        	}
        }
    });
    initData();

    function initData() {
        var goodsId = getUrlParam("goodsId");
        if (goodsId != "") {
            $.ajax({
                type: 'get',
                url: '/clyjScmGoods/' + goodsId,
                async: false,
                success: function (data) {
                    $("#goodsId").val(data.data.goodsId);
                    $("#goodsName").val(data.data.goodsName);
                    $("#goodsDepict").val(data.data.goodsDepict);
                    $("#goodsSpec").val(data.data.goodsSpec);
                    $("#originalPrice").val(data.data.originalPrice);
                    $("#presentPrice").val(data.data.presentPrice);
                    $("#pocketMoney").val(data.data.pocketMoney);
                    $("#brandId").val(data.data.goodsBrandId);
                    $("#supplierId").val(data.data.supplierId);
                    $("#postFee").val(data.data.postFee);
                    $("#img2").attr("src",data.data.goodsPic);
                    $("#img").attr("src",data.data.goodsBanner);
                    var list = data.data2.specDetail;
                    console.info(list);
                    if(list==null || list.length==0)
                    	return;
                    $("#col-md-10").empty();
                    for(var i=0;i<list.length;i++){
                    	if(i==0)
                    		$("#col-md-10").append('<br/><input type="text" value="'+list[i]+'"  placeholder="规格明细" name="specDetail" /><input value="'+data.data2.specNum[i]+'" type="text"  placeholder="库存数量" name="specNum" /><input type="text" value="'+data.data2.specSort[i]+'"  placeholder="排序" name="specSort" /><input type="button" class="addSpec" value="新增规格" />');
                    	else
                    		$("#col-md-10").append('<br/><div><input type="text" value="'+list[i]+'"  placeholder="规格明细" name="specDetail" /><input value="'+data.data2.specNum[i]+'" type="text"  placeholder="库存数量" name="specNum" /><input type="text" value="'+data.data2.specSort[i]+'"  placeholder="排序" name="specSort" /><button class="delSpec">删除规格</button></div>');
                    }
                }
            });

            initMenuDatas4(goodsId);
        }
    }


    $('#form').bootstrapValidator();

    //新增商品
    function add() {
        var bootstrapValidator = $("#form").data('bootstrapValidator');
        bootstrapValidator.validate();
        if (!bootstrapValidator.isValid()) {
            return;
        }
        var ids = getCheckedMenuIds2()
        $("form").ajaxSubmit({
			 type: 'post',
            url: '/clyjScmGoods/saveGoods',
            data:{"typeIds":ids},
            traditional: true,
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                   layer.msg("添加成功", {shift: -1, time: 1000}, function () {
                       location.href = "scmList.html";
                   });
               }
		})

    }
    //JS实现新增规格
    $(".addSpec").click(function(){
    	$(this).parent().append('<br/><div><input type="text"  placeholder="规格明细" name="specDetail" /><input type="text"  placeholder="库存数量" name="specNum" /><input type="text"  placeholder="排序" name="specSort" /><button class="delSpec">删除规格</button></div>');
    })
    //JS实现删除规格
    $("body").delegate(".delSpec",'click',function(){
    	$(this).parent().prev().remove();
    	$(this).parent().remove();
    })

</script>
</body>
</html>